﻿using MediatR;
using System.Reflection.Metadata;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;
namespace U.Enterprise.Api.Applications.Scalesetting
{
    public class DeductionsCreateHandler : IRequestHandler<DeductionsCreateCommond, Resqults>
    {

        private readonly IBateRepository<Deductions> _Deductions;

        public DeductionsCreateHandler(IBateRepository<Deductions> deductions)
        {
            _Deductions = deductions;
        }

        public async Task<Resqults> Handle(DeductionsCreateCommond request, CancellationToken cancellationToken)
        {
           

            var res=new Resqults() { Code=0};

                var query=_Deductions.GetAll().Where(x=>x.Ductionid==request.Ductionid).FirstOrDefault();
            if (query != null)
            {

                query.DuctionName=request.DuctionName;
                query.DEductionRemark = request.DuctionName;
                query.DeductionBusiness=request.DeductionBusiness;
               query.Stact=request.Stact;
                query.Ductionid=request.Ductionid;
                query.Price=request.Price;

              var gh=  _Deductions.Update(query);
                if(gh!=null)
                {
                    res.Code = 1;
                }


            }
            else
            {
                var list = new Deductions()
                {
                    Stact = request.Stact,
                    DeductionBusiness = request.DeductionBusiness,
                    DEductionRemark = request.DEductionRemark,
                    Ductionid = request.Ductionid,
                    DuctionName = request.DuctionName
                   ,Price = request.Price

                };
                var result = await _Deductions.Add(list);

                if (result > 0)
                {
                    res.Code = 1;
                }
            }
            

            return res;



        }
    }
}
